package com.zcily.demo.controller;

import com.zcily.demo.entity.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.lock.annotation.JLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
@Slf4j
@Api(tags = "分布式锁")
public class DistributedLockController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/lock/{key}")
    @JLock(lockKey = {"redis-lock", "#key"})
    @ApiOperation(value = "加锁测试")
    public Result<Object> get(@PathVariable("key") String key) {
        log.info("============ start lock ==============");
        Object value = redisTemplate.opsForValue().get(key);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("============ end lock ==============");
        return Result.ok(value);
    }
}